package org.tea.inventory.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tea.inventory.base.contants.UserConstants;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.dao.tea.*;
import org.tea.inventory.db.service.AlumniRecordStorageService;
import org.tea.inventory.domain.tea.*;
import org.tea.inventory.service.IProductService;
import org.tea.inventory.utils.DictUtils;
import org.tea.inventory.utils.UserUtil;

/**
 * 商品属性Service业务层处理
 * 
 * @author dongxb
 * @date 2025-04-07
 */
@Service
public class ProductServiceImpl implements IProductService
{
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private AlumniRecordStorageService storageService;

    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;

    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;
    /**
     * 查询商品属性
     * 
     * @param id 商品属性主键
     * @return 商品属性
     */
    @Override
    public Product selectProductById(Long id)
    {
        return productMapper.selectProductById(id);
    }

    /**
     * 查询商品属性列表
     * 
     * @param product 商品属性
     * @return 商品属性
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        product.setDelFlag(0);
        List<Product> products = productMapper.selectProductList(product);
        products.stream()
                .forEach(bean ->
                        {
                            ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(bean.getCategoryId());
                            if (productCategory != null) bean.setCategoryName(productCategory.getCategoryName());
                            if (bean.getBrandId() != null) bean.setBrandName(brandMapper.selectByPrimaryKey(bean.getBrandId().intValue()).getName());
                            bean.setStatusStr(bean.getStatus() == 0 ? "停用" : "启用");
                            bean.setCreateDateStr(DateUtil.format(bean.getCreateDate(), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
                            if (bean.getTeaFiles() != null) bean.setFiles(storageService.selectByPrimaryKeys(bean.getTeaFiles().split(",")));
                        }
                );
        return products;
    }

    /**
     * 新增商品属性
     * 
     * @param product 商品属性
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProduct(Product product)
    {
        product.setCreateDate(new Date());
        product.setCreateUserId(UserUtil.getUser().getLoginId());
        //插入商品信息
        int a=productMapper.insertProduct(product);
        if(a>0) {
            //查询所有仓库ID
            List<Long> warehouseIds = warehouseInfoMapper.findAllWarehouseInfoIds();
            //准备批量插入数据
            List<WarehouseInventory> relations = warehouseIds.stream()
                    .map(warehouseId -> new WarehouseInventory(
                            warehouseId,
                            product.getId(),
                            0L,
                            new Date()
                    )).collect(Collectors.toList());
            // 批量插入仓库库存基础信息
            if(!relations.isEmpty()) {
                warehouseInventoryMapper.batchInsertWarehouseInventory(relations);
            }
        }
        return a;
    }

    /**
     * 修改商品属性
     * 
     * @param product 商品属性
     * @return 结果
     */
    @Override
    public int updateProduct(Product product)
    {
        product.setUpdateDate(new Date());
        product.setUpdateUserId(UserUtil.getUser().getLoginId());
        //更新商品信息
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除商品属性
     * 
     * @param ids 需要删除的商品属性主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] ids)
    {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除商品属性信息
     * 
     * @param id 商品属性主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id)
    {
        return productMapper.deleteProductById(id);
    }

    /**
     * 检查商品编号是否重复
     *
     * @param product 商品属性
     * @return 结果
     */
    @Override
    public boolean checkProductCodeUnique(Product product) {
        Product info =  productMapper.selectProductById(product.getId());
        if (StringUtils.isNotNull(info)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 删除商品属性
     *
     * @param id 需要删除的商品属性主键
     * @return 结果
     */
    @Override
    public int deleteProductByCode(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setDelFlag(1);
        return productMapper.updateProduct(product);
    }
}
